using System;
using System.Collections.Generic;
using System.Text;
using RapidHDL;



namespace NegBinary
{
    public class NegBinary2CompPadFrame : PadFrame 
    {
        const int WIDTH = 8;
        NegBinary2Comp o2Comp;

        public override bool ConfigureSettings()
        {
            Settings.UseStart = false;
            Settings.BuildHardware = true;
            Settings.RunFromXilinx = true;
            Settings.ClockFrequency = 1;
            Settings.UseSDRAM = false;
            Settings.UseReset = false;
            Settings.BuildRemote = true;
            Settings.SynthASIC = false;
            Settings.SimulateVerilog = true;
            Settings.ForceBuildHardware = true;
            Settings.BuildServerPath = "desktop:8080";
            Settings.ClockPeriodNs = 7;
            return true;
        }

        public override bool GenerateStructure(TopLevelComponent TopLevelComponent)
        {

            o2Comp = new NegBinary2Comp(TopLevelComponent, "converter", WIDTH);

            SignalSource ssIn = SystemInterface.AddSignalSource("I_INPUT",o2Comp.I_INPUT,new string('0',WIDTH));
            SignalSource ssToNega = SystemInterface.AddSignalSource("I_TO_NEGA", o2Comp.I_TO_NEGA, new string('0', WIDTH));

            AssignPinCLK(39);
            AssignPinGND(40);
            AssignPinVDD(20);
            AssignPinInput("I_INPUT", 1);
            AssignPinInput("I_TO_NEGA", 9);
            return true;
        }

        public override bool ConfigureSignalViews(TopLevelComponent TopLevelComponent)
        {
            SystemInterface.AddSignalView("O_RESULT", "converter.O_RESULT");
            AssignPinOutput("O_RESULT", 21);
            return true;
        }

        private void ShowOutputs(HardwareScript poScript, int piTime)
        {
            //poScript.SignalView(piTime, "O_OVERFLOW");
            poScript.SignalView(piTime, "O_RESULT");
        }

        private void DoTest(HardwareScript poScript, int piTime, long piValue)
        {
            poScript.SignalOut(piTime, "I_INPUT", Convert.LongToNegBinary(piValue,WIDTH));
            poScript.SignalOut(piTime, "I_TO_NEGA", "0");
            poScript.SignalCheck(piTime + 1, "O_RESULT", Conversion.SignedLongToBinaryString(piValue,WIDTH + 2));
            poScript.SignalView(piTime + 1, "O_RESULT");

            poScript.SignalOut(piTime + 2, "I_INPUT", Conversion.SignedLongToBinaryString(piValue, WIDTH));
            poScript.SignalOut(piTime + 2, "I_TO_NEGA", "1");
            poScript.SignalCheck(piTime + 3, "O_RESULT", Convert.LongToNegBinary(piValue, WIDTH + 2));
            poScript.SignalView(piTime + 3, "O_RESULT");
        }

        public override bool GenerateHardwareScripts()
        {
            HardwareScript oScript = HardwareScripts.NewScript("test");
            int piTime = 1;


            DoTest(oScript, piTime+=4, 10);
            DoTest(oScript, piTime += 4, -8);
            DoTest(oScript, piTime += 4, -85);
            DoTest(oScript, piTime += 4, 85);
            DoTest(oScript, piTime += 4, -99);
            DoTest(oScript, piTime += 4, 7);
            DoTest(oScript, piTime += 4, -7);
            return true;
        }

    }
}
